<?php
namespace app\index\controller;

use think\facade\Session;
use app\index\controller\Base;
use app\common\model\User as UserModel;

class Login extends Base
{
    function index(){
        $this->redirect('index/index');
    }

    function login(){
        if(!$this->request->isPost()){
            $this->redirect('index/index');
        }
        if(!empty($this->current_user)){
            $this->success("您已登录", "index/index", 0);
        }

        $fields = [
            ['name' => 'name', 'type' => 'string', 'default' => ''],
            ['name' => 'password', 'type' => 'string', 'default' => ''],
        ];
        $data = $this->parsePostParam($fields);
        $Model = new UserModel();
        $login_res = $Model->doLogin($data["name"], $data["password"]);

        if(!$login_res['status']){
            $this->error($login_res['msg'], url('index/index'), 0);
        }else{
            Session::set("user", $login_res['data']);
            $this->success("登录成功", url('index/index'), 0);
        }
    }

    function logout(){
        if($this->request->isDelete()){
            Session::delete("user");
            $this->success('退出登录成功', url('index/index'), 0);
        }else{
            $this->error('访问页面不存在');
        }

    }
}
